package com.ushine.web.security.utils;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @Author: yls
 * @Date: 2021/8/19 10:41
 * @Description:
 * @Version 1.0
 */
@Component
@ConfigurationProperties(prefix = "rsa.key")
public class RsaKeyConfig {
    /**
     * 公钥文件
     */
    private String publicKeyFile;

    /**
     * 私钥文件
     */
    private String privateKeyFile;

    /**
     * 公钥
     */
    private PublicKey publicKey;

    /**
     * 私钥
     */
    private PrivateKey privateKey;

    /**
     * 秘钥
     */
    private String secret;

    @PostConstruct
    public void createRsaKey() throws Exception {
        RsaUtils.generateKey(publicKeyFile, privateKeyFile, secret);
        this.publicKey = RsaUtils.getPublicKey(publicKeyFile);
        this.privateKey = RsaUtils.getPrivateKey(privateKeyFile);
    }

    public String getPublicKeyFile() {
        return publicKeyFile;
    }

    public void setPublicKeyFile(String publicKeyFile) {
        this.publicKeyFile = publicKeyFile;
    }

    public String getPrivateKeyFile() {
        return privateKeyFile;
    }

    public void setPrivateKeyFile(String privateKeyFile) {
        this.privateKeyFile = privateKeyFile;
    }

    public PublicKey getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(PublicKey publicKey) {
        this.publicKey = publicKey;
    }

    public PrivateKey getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(PrivateKey privateKey) {
        this.privateKey = privateKey;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    @Override
    public String toString() {
        return "RSAKeyConfig{" +
                "publicKeyFile='" + publicKeyFile + '\'' +
                ", privateKeyFile='" + privateKeyFile + '\'' +
                ", publicKey=" + publicKey +
                ", privateKey=" + privateKey +
                ", secret='" + secret + '\'' +
                '}';
    }
}
